package com.lj.sys.service.impl;

import com.lj.sys.entity.SysLogEntity;
import com.lj.sys.mapper.SysLogMapper;
import com.lj.sys.service.SysLogService;
import com.lj.mp.standard.StandardServiceImpl;
import org.springframework.stereotype.Service;
import com.lj.mp.utils.PageQueryUtils;
import cn.hutool.core.util.ObjectUtil;
import com.lj.sys.result.SysLogInfoResult;
import com.lj.sys.result.SysLogPageResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lj.sys.param.SysLogPageParam;
import java.io.OutputStream;
import java.util.List;
import com.lj.common.utils.excel.ExcelSheet;
import com.lj.common.utils.excel.ExcelUtil;
import com.lj.sys.result.SysLogExportResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * <p>
 * 系统日志 服务实现类
 * </p>
 *
 * @author luojing
 * @since 2025-04-18 17:22:24
 */
@Service
public class SysLogServiceImpl extends StandardServiceImpl<SysLogMapper, SysLogEntity> implements SysLogService {

    @Override
    public IPage<SysLogPageResult> page(SysLogPageParam param) {
        return this.page(PageQueryUtils.getPage(param), getQueryWrapper(param)).convert(SysLogPageResult::of);
    }

    private LambdaQueryWrapper<SysLogEntity> getQueryWrapper(SysLogPageParam param) {
        return lambdaQueryWrapper()
                .eq(ObjectUtil.isNotEmpty(param.getLogName()), SysLogEntity::getLogName, param.getLogName())
                .eq(ObjectUtil.isNotEmpty(param.getSucceed()), SysLogEntity::getSucceed, param.getSucceed())
                ;
    }

    @Override
    public SysLogInfoResult info(Long id) {
        return SysLogInfoResult.of(this.getById(id));
    }
    @Override
    public void exportExcel(SysLogPageParam param, OutputStream out) {
        List<SysLogEntity> dataList = this.list(getQueryWrapper(param));
        List<SysLogExportResult> exportList = dataList.stream().map(SysLogExportResult::of).toList();
        ExcelUtil.writeExcel(out, new ExcelSheet<>(exportList, SysLogExportResult.class));
    }
}
